package org.example.test.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.test.entity.Position;
import org.example.test.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/position")
public class PositionController {
    @Autowired
    private PositionService positionService;

    /**
     * 查询
     * @return
     */
    @RequestMapping("/positions")
    public Map<String, Object> positions(Integer pageNum, Integer pageSize, String positionName) {
        //既存放数据，又存放总条数
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Object> startPage = PageHelper.startPage(pageNum, pageSize);
        map.put("positionList", positionService.findAll(positionName));//向容器中放获取到的数据
        map.put("total", startPage.getTotal());//向容器中放数据总条数
        return map;
    }
    /**
     * 删除
     * @param positionId
     * @return
     */
    @RequestMapping("/delete")
    public int delete(Integer positionId){
        return positionService.delete(positionId);
    }

    /**
     * 修改
     * @param position
     * @return
     */
    @RequestMapping("/update")
    public int update(@RequestBody Position position)
    {
        return positionService.update(position);
    }
    @RequestMapping("/add")
    public int add(@RequestBody Position position){
        return positionService.add(position);
    }
}
